﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
namespace eTruck.Views.Supplier
{
    public partial class List : System.Web.UI.Page
    {
        eTruck.BLL.Supplier bll = new BLL.Supplier();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                HiddenField1.Value = "";
                HiddenField2.Value = "";
                HiddenField3.Value = "";
              
                btnDelete.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？\")");
                
                BindData();
            }
        }
        public void BindData()
        {


            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat("CorporationName like '%{0}%'", txtKeyword.Text.Trim().Replace("'", ""));
            }
            ds = bll.GetAllList(); 
            gridView.DataSource = ds;
            gridView.DataBind();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            try
            {
                bll.DeleteList(idlist);
            }
            catch
            {
                HiddenField1.Value = "删除供应商信息";
                HiddenField2.Value = "有些供应商信息不能删除";
                HiddenField3.Value = "error";
            }
            BindData();
        }

        #region gridView



        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((Button)e.Row.Cells[7].Controls[0]).Attributes.Add("onclick", "javascript:if(!confirm('你确认要删除 \"" + e.Row.Cells[1].Text + "\"?')) return;");
                }
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = int.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "DeleteRow")
            {
                int id = Convert.ToInt32(gridView.DataKeys[index]["CorporationID"].ToString());
                try
                {
                    if (bll.Delete(id))
                    {
                        BindData();
                    }
                    else
                    {
                        //
                        //Response.Write("<script language='javascript'>alert('Can't Delete! '); </script>");
                        //helper.MessageBox(Page, "'Can't Delete!" + result, "#");
                    }
                }
                catch
                {
                    HiddenField1.Value = "删除供应商信息";
                    HiddenField2.Value = "此供应商信息不能删除";
                    HiddenField3.Value = "error";
                }
            }
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

    }
}
